//============================================================================
// Name        : AnchorText.cpp
// Author      : lucmir@dcc.ufmg.br
// Description :
//============================================================================

#include "AnchorText.h"


AnchorText::AnchorText() {
}

AnchorText::~AnchorText() {
	std::map<unsigned int, std::map<unsigned int, unsigned int> *>::iterator itr;
	for (itr=this->terms.begin(); itr!=this->terms.end(); itr++) {
		itr->second->clear();
		delete itr->second;
	}
	this->terms.clear();
}

unsigned int AnchorText::containsTerm(unsigned int termId, unsigned int docId) {
	std::map<unsigned int, std::map<unsigned int, unsigned int> *>::iterator itr;
	std::map<unsigned int, unsigned int>::iterator inItr;
	itr = this->terms.find(termId);
	if ( itr == this->terms.end() ) {
		return 0;
	}
	inItr = itr->second->find(docId);
	if ( inItr == itr->second->end() ) {
		return 0;
	}
	return inItr->second;
}

void AnchorText::insertTerm(unsigned int termId, unsigned int docId) {
	std::map<unsigned int, std::map<unsigned int, unsigned int> *>::iterator itr;
	std::map<unsigned int, unsigned int>::iterator inItr;
	std::map<unsigned int, unsigned int> *value;

	itr = this->terms.find(termId);
	if ( itr == this->terms.end() ) {
		value = new std::map<unsigned int, unsigned int>();
		this->terms[termId] = value;
	}
	else {
		value = itr->second;
	}
	inItr = value->find(docId);
	if ( inItr == value->end() ) {
		(*value)[docId] = 1;
	}
	else {
		(*value)[docId]++;
	}
}

void AnchorText::insertTerm(unsigned int termId, unsigned int docId, unsigned int freq) {
	std::map<unsigned int, std::map<unsigned int, unsigned int> *>::iterator itr;
	std::map<unsigned int, unsigned int>::iterator inItr;
	std::map<unsigned int, unsigned int> *value;

	itr = this->terms.find(termId);
	if ( itr == this->terms.end() ) {
		value = new std::map<unsigned int, unsigned int>();
		this->terms[termId] = value;
	}
	else {
		value = itr->second;
	}
	(*value)[docId]++;
}

bool AnchorText::printToFile(std::string outFileName) {
	std::ofstream file;
	std::map<unsigned int, std::map<unsigned int, unsigned int> *>::iterator itr;
	std::map<unsigned int, unsigned int>::iterator inItr;

	file.open(outFileName.c_str());
	if(!file.is_open()) {
		std::cout << "\nERRO: Não foi possível abrir o arquivo: " << outFileName << "\n";
		return false;
	}
	for (itr=this->terms.begin(); itr!=this->terms.end(); itr++) {
		for(inItr=itr->second->begin(); inItr!=itr->second->end(); inItr++) {
			file << itr->first << " " << inItr->first << " " << inItr->second;
			file << "\n";
		}
	}
	file.close();

	return true;
}

bool AnchorText::readFromFile(std::string inFileName) {
	std::ifstream file;
	std::string line;
	unsigned int termId, docId, freq;

	// Document file
	file.open(inFileName.c_str());
	if (!file.is_open()) {
		std::cout << "\nERRO: Não foi possível abrir o arquivo: \"" << inFileName << "\"\n\n";
		return false;
	}
	while(!file.eof()) {
		getline(file, line);
		std::istringstream istr(line);
		istr >> termId;
		istr >> docId;
		istr >> freq;
		if(!file.eof()) {
			this->insertTerm(termId, docId, freq);
		}
	}
	file.close();

	return true;
}
